import React from 'react';
import { Toast } from 'antd-mobile';

import path from '@/config/path';

/**
 * IsLogin decorator
 * @param Main Component
 * @return Component Class
 */
export default (Main) => {
    return class extends React.Component{
        componentDidMount(){
            this.isLogin === undefined && this.props.userStaus();

            if(!this.props.history || !this.props.userStaus){
                throw new Error('please add @connect(state => state.user,{ userStaus }),@withRouter on this decorator before.');
            }
        }
        componentWillReceiveProps(props, state){
            const { location, history, isLogin } = props;
            if(location.pathname.indexOf(path['30002']) > -1 && !isLogin ){
                Toast.fail('未登录或者登录超时', 1, () => {history.push(path['30003'])})
            } else if(location.pathname.indexOf(path['30002']) === -1 && isLogin){
                history.push(path['30002']);
            }
        }
        render(){
            return <Main {...this.props} />
        }
    }
}

